package com.pluginsacceptor.plugin.spreadsheet.menu;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.apache.log4j.Logger;

public class MenuSS {
	private JLabel display;
	private static final Logger LOG = Logger.getLogger(MenuSS.class);
	static JMenuItem itemOfSSDynamicMenu_1;
	static JMenuItem itemOfSSDynamicMenu_2;
	final static ImageIcon principalIcon = new ImageIcon("imgs/icon.png") ;
	final static ImageIcon sumIcon = new ImageIcon("imgs/sum.png");
	private JTable chooserWindow;

	private JMenuItem [] creatItemsForSSMenu() {
		itemOfSSDynamicMenu_1 = new JMenuItem("Open");
		itemOfSSDynamicMenu_1.setIcon(principalIcon);
		itemOfSSDynamicMenu_1.addActionListener(
				new ActionListener() {
					//Handle JMenuItem event if mouse is clicked.
					@Override
					public void actionPerformed(final ActionEvent event) {
						final JFileChooser chooser = new JFileChooser();
						chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
						chooser.setCurrentDirectory(new File("Plugins-acceptor"));
						// TODO filter donsn't work?
						final FileNameExtensionFilter filter = new FileNameExtensionFilter("jar","zip");
						chooser.setFileFilter(filter);
						final int returnVal = chooser.showOpenDialog(chooserWindow);
						if (returnVal == JFileChooser.APPROVE_OPTION) {
							final String path = chooser.getSelectedFile().getAbsolutePath();
							LOG.info("You chose to open this file '" + path + "'");
						}
					}});
		itemOfSSDynamicMenu_2 = new JMenuItem("Save");
		itemOfSSDynamicMenu_2.setIcon(sumIcon);
		itemOfSSDynamicMenu_2.addActionListener(
				new ActionListener() {
					//Handle JMenuItem event if mouse is clicked.
					@Override
					public void actionPerformed(final ActionEvent event) {
					//	final SSData spreadsheetData = new SSData();
					//	final JTable ssTable = SSActivator.ssTable;
						final JFileChooser chooser = new JFileChooser();
						chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
						chooser.setCurrentDirectory(new File("Plugins-acceptor"));
						// TODO filter donsn't work?
					//	final FileNameExtensionFilter filter = new FileNameExtensionFilter("jar","zip");
					//	chooser.setFileFilter(filter);
						final int returnVal = chooser.showOpenDialog(chooserWindow);
						if (returnVal == JFileChooser.APPROVE_OPTION) {
							final String path = chooser.getSelectedFile().getAbsolutePath();
							LOG.info("You chose to open this file '" + path + "'");
						}
					}});






		final JMenuItem [] tabOfItems = new JMenuItem []{
				itemOfSSDynamicMenu_1,itemOfSSDynamicMenu_2
		};
		return tabOfItems;
	}
	public static void main(final String[] args) {
		final JFrame prinicpalWindow = new JFrame();
		prinicpalWindow.setTitle("PLUGIN MANAGER SPREADSHEET");
		prinicpalWindow.setBounds(100, 100, 1000, 600);
		prinicpalWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		final JMenuBar menuBar = new JMenuBar();
		prinicpalWindow.setJMenuBar(menuBar);
		final JMenu dynamicMenu = new JMenu("Menu");
		dynamicMenu.setIcon(principalIcon);
		dynamicMenu.addSeparator();
		menuBar.add(dynamicMenu);
		final MenuSS item1 = new MenuSS();
		final JMenuItem [] myItems =item1.creatItemsForSSMenu();
		for (final JMenuItem item :myItems){
			dynamicMenu.add(item);
		}
		final JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		prinicpalWindow.add(panel, BorderLayout.CENTER);
		prinicpalWindow.setVisible(true);
	}
}